package cn.edu.tsinghua.ie.ieFunds.controller;

import cn.edu.tsinghua.ie.ieFunds.bean.base.Stock;
import cn.edu.tsinghua.ie.ieFunds.bean.bussiness.Fund;
import cn.edu.tsinghua.ie.ieFunds.bean.bussiness.FundStock;
import cn.edu.tsinghua.ie.ieFunds.bean.bussiness.Transaction;
import cn.edu.tsinghua.ie.ieFunds.bean.system.User;
import cn.edu.tsinghua.ie.ieFunds.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("fund")
public class FundController {

    @Autowired
    private StockService stockService;

    @Autowired
    private FundService fundService;

    @Autowired
    private TransactionService transactionService;

    @Autowired
    private FundStockService fundStockService;

    @Autowired
    private UserService userService;

    @RequestMapping(value = "getFund", method = RequestMethod.GET)
    public Fund getFund() {
        UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext()
                .getAuthentication()
                .getPrincipal();
        String userName = userDetails.getUsername();
        return fundService.getFundByUserName(userName);
    }

    @RequestMapping(value = "add", method = RequestMethod.POST)
    public void add(@RequestBody Fund fund, int[] quantity) {
        fund.setCreateDate(new Date());
        UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext()
                .getAuthentication()
                .getPrincipal();
        String userName = userDetails.getUsername();
        User user = userService.getUserByUsername(userName);
        fund.setUsername(userName);

        List<FundStock> fundStockList = new ArrayList<>();
        List<Stock> stockList = stockService.getDistinctList();
        Date now = new Date();
        for (Stock stock : stockList) {
            int index = stockList.indexOf(stock);
            if (quantity[index] <=0) continue;

            // generate fund stock
            FundStock fundStock = new FundStock();
            fundStock.setStock(stock);
            fundStock.setStockQuantity(quantity[stockList.indexOf(stock)]);
            fundStockService.save(fundStock);
            fundStockList.add(fundStock);

            // update stock
            stock.setRemainQuantity(stock.getRemainQuantity() - quantity[index]);

            // save transaction
            Transaction transaction = new Transaction();
            transaction.setPrice(stock.getPrice());
            transaction.setQuantity(quantity[index]);
            transaction.setStock(stock);
            transaction.setTime(now);
            transaction.setUser(user);
            transactionService.add(transaction);

            // update fund cash
            //FIXME if the cash is negative
            fund.setCash(fund.getCash() - stock.getPrice() * quantity[index]);
        }
        fund.setFundStocks(fundStockList);
        fundService.add(fund);
    }
}
